// src/routes/securities/+page.ts
import type { GetParams, IpcResponse, ListParams, TradeBot, TradeBotFilter, TradeStrategy } from '$lib/bindings';
import type { PageLoad } from './$types';
import * as commands from '$lib/commands'

export const load: PageLoad = async ({ url, route, params }) => {
    const { id } = params;
    console.log("(strategies/[id]/+page.ts) route=", route, " url=", url, " params=", params);
    try {

        // Define the argument
        const args: GetParams = {
            id: id, // "Tstg.DAY.0",
        }

        const res: IpcResponse<TradeStrategy> = await commands.get_trade_strategy(args)

        console.log("(strategies/[id]/+page.ts) res=", res, " args=", args);

        if (res.error) {
            // If the response status is not OK, throw an error
            throw new Error(`(strategies/[id]/+page.ts) Server returned an error: ${res.error}`);
        }

        // If the server response is empty, return sample data
        if (res.result) {
            return {
                id: id,
                tradeStrategy: res.result.data
            };
        }

        return {
            id: id,
            tradeStrategy: null
        };
    } catch (error) {
        // If there's any error (e.g., network issue), return sample data
        console.error('(strategies/[id]/+page.ts) Error fetching tbots:', error);
        return {
            id: id,
            tradeStrategy: null
        };
    }
};